package com.zy.study.service.serviceImpl;

import java.util.ArrayList;
import java.util.List;


import org.springframework.stereotype.Service;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zy.study.pojo.CountryData;
import com.zy.study.pojo.TodayData;
import com.zy.study.service.GetDataService;
import com.zy.study.utils.MyJsonUtil;
import com.zy.study.utils.MyStringUtils;
import com.zy.study.utils.ResponseCode;
import com.zy.study.utils.MyException.BizException;

@Service
public class GetDataServiceImpl implements GetDataService {
	/**
	 *根据Json字符串获取各个国家的数据
	 */
	@Override
	public List<CountryData> getDataListByUrl(String apiUrl){
		try {
			//得到数据的json字符串
			String jsonString=MyJsonUtil.getJsonByApi(apiUrl);
			
			JsonParser parser = new JsonParser();
			JsonElement element = parser.parse(jsonString);
			JsonObject root = element.getAsJsonObject();
			//获取到的所有data
			JsonObject data = root.getAsJsonObject("data");
			//得到整个areaTree的数据
			JsonElement e1=data.get("areaTree");
			
			//得到areaTree数据的jsonarray
			JsonArray jsonArray=e1.getAsJsonArray();
			
			JsonObject todayJsonObject;
			
			JsonObject totalJsonObject;
			
			List<CountryData> listCountryData=new ArrayList<CountryData>();
			//解析json数据
			for(int i=0;i<jsonArray.size();i++){
				//今日数据的jsonobject对象
				todayJsonObject=jsonArray.get(i).getAsJsonObject().get("today").getAsJsonObject();
				//设置今日的数据对象
				TodayData todayData=new TodayData(
						MyStringUtils.checkStrIsNum(todayJsonObject.get("confirm").toString())?todayJsonObject.get("confirm").getAsInt():0, 
						MyStringUtils.checkStrIsNum(todayJsonObject.get("suspect").toString())?todayJsonObject.get("suspect").getAsInt():0, 
						MyStringUtils.checkStrIsNum(todayJsonObject.get("heal").toString())?todayJsonObject.get("heal").getAsInt():0, 
						MyStringUtils.checkStrIsNum(todayJsonObject.get("dead").toString())?todayJsonObject.get("dead").getAsInt():0
										);
				//累计数据的jsonobject对象
				totalJsonObject=jsonArray.get(i).getAsJsonObject().get("total").getAsJsonObject();
				//国家累计数据对象
				CountryData countryData=new CountryData(
						jsonArray.get(i).getAsJsonObject().get("id").getAsString(), 
						jsonArray.get(i).getAsJsonObject().get("name").getAsString(), 
						MyStringUtils.checkStrIsNum(totalJsonObject.get("confirm").toString())?totalJsonObject.get("confirm").getAsInt():0, 
						MyStringUtils.checkStrIsNum(totalJsonObject.get("suspect").toString())?totalJsonObject.get("suspect").getAsInt():0, 
						MyStringUtils.checkStrIsNum(totalJsonObject.get("heal").toString())?totalJsonObject.get("heal").getAsInt():0, 
						MyStringUtils.checkStrIsNum(totalJsonObject.get("dead").toString())?totalJsonObject.get("dead").getAsInt():0, 
						todayData, 
						jsonArray.get(i).getAsJsonObject().get("lastUpdateTime").getAsString(),
						null,null
						);
				
				listCountryData.add(countryData);
			}
			
			return listCountryData;
			
		} catch (Exception e) {
			throw new BizException(ResponseCode.FAILED.getCode(),"服务器正忙");
		}
			
	}	
}
